// pages/coupons-details/coupons-details.js
Page({
    onShareAppMessage (options) {   //分享好友
        this.setData({
            isShare:1
        })
        return {
            title:this.data.shareText,
            path: `/pages/shareCoupon/shareCoupon?share_user=${this.data.userMessage.id}&userName=${this.data.userMessage.user_nickname}&id=${this.data.allInfo.id}`,
            imageUrl: 'https://2am-project.cn/img/20220524/shareCard.png',   //分享图片
        }
    },
    /**
     * 页面的初始数据
     */
    data: {
        
        temIds:['Wzq0Q8OpleP_KgHNxStTKJ6-8NdvRvqgJnioZ01w7bg','IKdsAyES45qURTGEQMaHqLRq-fpoaU83kNRtz3TcC78'],
        allInfo:{},
        userMessage:{}, 
        id:'',      //优惠券id
        couponInfo:{},    //优惠券详情
        userInfo:{},
        shareText:'邀您开启精准护肤之旅',  //分享内容
        showShare:false,
        isShare:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options&&options.id){
            this.setData({
                id:options.id
            })
            this.getInfo()
        }
    },
    setInfo(){// 订阅消息
        let that = this
        this.setData({
          showShare:false,
          isShare:0
        })
        let shareData={
          user_id:that.data.userInfo.id,
          type:3,
          other_type:'',
        }
        wx.requestSubscribeMessage({
            tmplIds: that.data.temIds,
            success (res) {
              let arr = []
                if(res[that.data.temIds[0]]=='accept'){//拒绝
                  arr.push('1')
                }
                if(res[that.data.temIds[1]]=='accept'){
                  arr.push('2')
                }
                // else if(res[that.data.temIds[2]]=='accept'){
                  // arr.push('3')
                // }else if(res[that.data.temIds[3]]=='accept'){  //允许
                  // arr.push('4')
                // }
                if(arr.length>0){
                  shareData.other_type = arr.join(',')
                  that.sendSetYue(shareData)
                }
            },
            fail(error){
              console.log(error,'123失败');
            }
          })
      },
      sendSetYue(data){
        wx.$api.sendTemplate(data).then(res=>{
          if(res.code>0){
          }else{
              wx.showToast({
                title: res.msg,
                icon:'none'
              })
          }
        })
    },
    getInfo(){
        var that = this
        wx.$api.getCouponInfo({
                coupon_user_id: that.data.id
            }).then(res=>{
              if(res.code==1){
                  that.setData({
                      allInfo:res.data,
                      couponInfo: res.data.coupon_info
                  })
              }else{
                  wx.showToast({
                    title: res.msg,
                  })
              }
        })
    },
    handleClick () {
        console.log('32')
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var uinfo  = wx.getStorageSync('uinfo')
        if(uinfo&&uinfo.user){
            let n = uinfo.user.user_nickname || ''
          let title =  n + '邀您开启精准护肤之旅'
            this.setData({
                shareText: title,
                userInfo:uinfo.user,
                userMessage:uinfo.user
            })
        }
        // if(this.data.isShare>0){
        //     this.setData({
        //         showShare:true
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    toExclusive(){
        wx.switchTab({
          url: '../exclusive/exclusive',
        })
    }
})